package com.chang.bootplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chang.bootplus.base.exception.BusException;
import com.chang.bootplus.base.utils.ApiResponse;
import com.chang.bootplus.base.utils.BeanUtil;
import com.chang.bootplus.entity.Menu;
import com.chang.bootplus.entity.School;
import com.chang.bootplus.mapper.MenuMapper;
import com.chang.bootplus.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chang.bootplus.service.SchoolService;
import com.chang.bootplus.vo.MenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chang
 * @since 2022-01-23
 */
@Service
public class MenuServiceImp extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    SchoolService schoolService;

    @Resource
    MenuMapper mapper;

    @Override
    public ApiResponse<MenuVO> listMenu() {

        String  username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long userId = schoolService.getOne(new LambdaQueryWrapper<School>().eq(School::getNickName, username)).getId();
        List<Long> menuIds = mapper.getMenuIds(userId);
        if (menuIds==null){
            new BusException("管理员无菜单项");
        }
        // 获取到所有菜单
        List<Menu> menus = listByIds(mapper.getMenuIds(userId));

        // 获取到了父菜单
        List<Menu> root = menus.stream().filter(t -> t.getParentId() == 0).collect(Collectors.toList());

        // 获取父菜单的子菜单
        root.stream().forEach(t->{
            List<MenuVO> menuVOS = BeanUtil.listA2ListB(getChlidrenList(t.getId(), menus), MenuVO.class);
            t.setChildren(menuVOS);
        });
        List<MenuVO> menuVOS = BeanUtil.listA2ListB(root, MenuVO.class);

//        System.out.println(menuVOS);
        // 获取权限信息集合
        String authorityInfo = schoolService.getUserAuthorityInfo(userId);// ROLE_admin,ROLE_normal,sys:user:list,....
        List<String> authorityInfoList = Arrays.stream(StringUtils.tokenizeToStringArray(authorityInfo, ",")).collect(Collectors.toList());

        return new ApiResponse<>(menuVOS).setAuthorityist(authorityInfoList);
    }

    private  List<Menu> getChlidrenList(Long id, List<Menu> menus) {
        // Long型的比较 不能用==号 // 去全部id找对应关系
        List<Menu> childrenlist = menus.stream().filter(t1 -> t1.getParentId().longValue() == id.longValue()).collect(Collectors.toList());

        // 递归出口
        if (childrenlist.size()==0){
            return null;
        }
        // 递归调用获取
        childrenlist.stream().forEach(t->{
            List<MenuVO> menuVOS = BeanUtil.listA2ListB(getChlidrenList(t.getId(), menus), MenuVO.class);
            t.setChildren(menuVOS);
        });

        return childrenlist;
    }

}
